package org.dlsu.mt.parser.dictionary;

/**
 * made by LEFT thesis group
 *
 * monolingual dictionary module
 *
 * Function class
 * representation for a function that will be used for a verb
 * this function is represented as a vector of symbols in the form of strings
 */

import java.util.StringTokenizer;
import java.util.Vector;

public class Function
{
	private Vector<String> symbols; //storage for the symbols in the function
	private int counter; //counter for the number of elements in the vector
	
	public Function(String symbol)
	{
		/**
		 * constructor that instantiates the function by passing the function through the parameter as one whole string
		 * this string is then parsed and separated by spaces to be inserted to the vector
		 */
		parse(symbol);
		counter = 0;
	}
	
	public void parse(String symbol)
	{
		/**
		 * parses the string and separates it by spaces
		 * these symbols are then inserted into the vector
		 */
		symbols = new Vector();
		StringTokenizer tk = new StringTokenizer(symbol, " ");
		while(tk.hasMoreTokens())
		{
			String temp = tk.nextToken();
			symbols.addElement(temp);
		}
	}
	
	public String getSymbols()
	{
		/**
		 * returns the symbols as a single string
		 */
		StringBuffer temp = new StringBuffer();
		for(int i=0;i<symbols.size();i++)
		{
			if(i == 0)
			{
				temp.append(symbols.get(i));
			}
			else
			{
				temp.append(" " + symbols.get(i));
			}
		}
		return temp.toString();
	}
	
	public String getNextSymbol()
	{
		/**
		 * returns the next symbol from the vector of symbols
		 * increments the current symbol
		 */
		if(counter<symbols.size())
		{
			counter++;
			return symbols.get(counter-1);
		}
		else
			return null;
	}
	
	public boolean hasMoreSymbols()
	{
		/**
		 * returns a boolean value whether more symbols can be found in the vector of symbols
		 *
		 * returns true if more symbols are found
		 * returns false if it has passed the last element
		 */
		if(counter<symbols.size())
			return true;
		else
			return false;
	}
	
	public int getNumSymbols()
	{
		/**
		 * returns the number of symbols found in the vector
		 */
		return symbols.size();
	}
	
	public boolean isFound(String symbol)
	{
		/**
		 * scans through the vector of symbols and tries to locate the symbol specified through the parameter
		 * this is used for determining whether the terminal or non-terminal symbol is found in the function
		 */
		for(int i = 0;i<symbols.size();i++)
		{
			if(symbols.get(i).equals(symbol))
				return true;
		}
		return false;
	}
	
	public int findPos(String symbol)
	{
		/**
		 * scans the vector and tries to locate the symbols indicated through the parameter
		 * returns the position where it is found in the vector where the position starts with 1
		 * returns 0 if the symbols is not found
		 */
		for(int i = 0;i<symbols.size();i++)
		{
			if(symbols.get(i).equals(symbol))
				return i+1;
		}
		return 0;
	}
	
	public int compare(String sym1, String sym2)
	{
		/**
		 * compares the position of both symbols
		 * returns 1 if symbol 1 comes first in the function
		 * returns 2 if symbol 2 comes first in the function
		 * returns 3 if both share the same position (sym1 == sym2)
		 * returns 0 if either symbol cannot be found in the vector
		 */
		int pos1 = findPos(sym1);
		int pos2 = findPos(sym2);
		
		if(pos1!=0&&pos2!=0)
		{
			if(pos1>pos2)
				return 2;
			else if(pos1<pos2)
				return 1;
			else
				return 3;
		}
		else
		{
			return 0;
		}
	}
	
	public void resetSymbols()
	{
		/**
		 * resets the counter in order to point to the first element in the vector of symbols
		 */
		counter = 0;
	}
}